package com.lixin.model.response;

/**
 * Description:
 * Copyright:   Copyright (c)2023
 * Company:     sci
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2023-07-24 23:03:42
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2023-07-24     张李鑫                     1.0         1.0 Version
 */
public class Response<T> {

    private static final long serialVersionUID = 3136093880967040057L;
    protected T model;
    protected int code;
    protected String message;


    public Response() {
        this.model = null;
        this.code = 0;
        this.message = "";
    }

    public Response(T model) {
        this.code = 0;
        this.message = "";
        this.model = model;
    }

    public Response(int code, String message) {
        this.model = null;
        if (code > 0) {
            code *= -1;
        }

        this.code = code;
        this.message = message;
    }


    public Response(int code) {
        this(code, "failed");
    }

    public static <T> Response<T> error(int code, String message) {
        return new Response(code, message);
    }

    public static <T> Response<T> error(String message) {
        return error(-1, message);
    }


    public static <T> Response<T> success(T object) {
        Response<T> res = new Response<>(object);
        res.code = 1;
        res.message = "successed";
        return res;
    }

    public static <T> Response<T> success() {
        Response<T> res = new Response<>();
        res.code = 1;
        res.message = "successed";
        return res;
    }

//
//    public static <T> Response<T> success(String event) {
//        Response<T> res = new Response<>();
//        res.code = 1;
//        res.message = "successed";
//        res.event = event;
//        return res;
//    }

    public boolean successed() {
        return this.code >= 0;
    }

    public boolean failed() {
        return !this.successed();
    }

    public T getModel() {
        return this.model;
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    @Override
    public String toString() {
        return "Response{" +
                "model=" + model +
                ", code=" + code +
                ", message='" + message + '\'' +
                '}';
    }
}
